package tools;

/*
 * Author: Lap Ho Cheung
 * Edited By: Samantha Choy
 * Date: August 11, 2014
 * File name: Validator.java
 * Function: A collection of methods to validate user input in EventInputI.java
 * Input: date, time, or start/end date-time in accordance with .ics file standards
 * Output: boolean values
 * 
 * More info about this project can be found at https://code.google.com/p/ics314-sum14-amur-cal/
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Validator {
	
	public static boolean isValidChoice (int minValidChoice, int maxValidChoice, int choice) {
		if (choice < minValidChoice || choice > maxValidChoice) {
	        return false;
	    }// end if
		else {
			return true;
		}
	}
	
	public static boolean isValidPriority (int newPriority) {
		if (newPriority < 0 || newPriority > 9) {
			return false;
		}
		else {
			return true;
		}
	}

	public static boolean isValidDate (String date){
			
		//Testing if the input of data is blank
		if ((date == null)){
			return false;
		}
		
		//Date format
		SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
		formatter.setLenient(false);
		
		//Testing
		try {
			Date eventDate = formatter.parse(date);			
		}
		catch (ParseException e){
			return false;
		}
		
		return true;
	}

	
	/*
	 * Hours are from 0-23
	 */
	public static boolean isValidTime (String time){
		
		//Testing if the input of data is blank
		if ((time == null)){
			return false;
		}
		
		//Date format
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		formatter.setLenient(false);
		
		//Testing
		try {
			Date eventtime = formatter.parse(time);			
		}
		catch (ParseException e){
			return false;
		}
		
		return true;
	}
	
	public static boolean isValidEvent (String start, String end){
		
		//Testing if the input of data is blank
		if ((start == null) || (end == null)){
			return false;
		}
		
		//Date format
		SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
		formatter.setLenient(false);
		
		//Testing
		try {
			Date startEvent = formatter.parse(start);
			Date endEvent = formatter.parse(end);
			
			//Return "false" if startEvent and endEvent are the same
			if (startEvent.equals(endEvent)){
				return false;				
			}
			
			//Return "false" if startEvent is after endEvent
			if (startEvent.after(endEvent)){
				return false;
			}
			
		}
		catch (ParseException e){
			return false;
		}
		
		return true;
	}
}
